const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    is_sign: 0, // 是否签到
    sign_records:{},
    sign_config:{},
    data_y:0,
    data_m:0,
    data_d:0,
    data_w:0,
    is_load: 0,
    signInFlag: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.defaultImg(this, true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  loadInfo: function() {
    let that = this;
    let current_time = that.data.current_time;

    app.sendRequest({
      url: 'NsMemberSign.MemberSign.isSignin',
      data: {},
      success: function(res) {
        if (res.code == 0) {
          let is_sign = app.checkEmpty(res.data, 0);

          that.setData({
            is_sign: is_sign,
            is_load: 1
          })
        }
      }
    })
    let timestamp = Date.parse(new Date());
    let date = new Date(timestamp);

    let data_y = date.getFullYear();
    //获取月份  
    let data_m = date.getMonth() + 1;
    //获取当日日期 
    let data_d = date.getDate();

    let data_one = new Date(data_y, data_m - 1, 1);
    let data_w = data_one.getDay(data_one);

    that.setData({
      data_y: data_y,
      data_m: data_m,
      data_d: data_d,
      data_w: data_w
    })

    that.loadSignConfig();
    that.loadSignRecords();
  },

  /**
   * 加载签到配置
   */
  loadSignConfig: function () {
    let that = this;

    app.sendRequest({
      url: 'NsMemberSign.MemberSign.getSignInConfig',
      data: {},
      success: function (res) {
        if (res.code == 0) {
          let sign_config = app.checkEmpty(res.data, {});

          that.setData({
            sign_config: sign_config
          })
        }
      }
    })
  },

  /**
   * 加载签到记录
   */
  loadSignRecords: function(){
    let that = this;

    app.sendRequest({
      url: 'NsMemberSign.MemberSign.signInRecords',
      data: {},
      success: function(res) {
        if (res.code == 0) {
          let data_array = app.checkEmpty(res.data, {});
          let sign_records = [];
          let index = 0;
          Object.keys(data_array).sort().forEach(function (k) {
            sign_records[index] = {};
            sign_records[index].day = k;
            sign_records[index].is_sign = data_array[k];
            index++;
          })

          that.setData({
            sign_records: sign_records
          })
        }
      }
    })
  },

  /**
   * 去签到
   */
  signIn:function(){
    let that = this;
    let signInFlag = that.data.signInFlag;
    if (signInFlag == 1) {
      return false;
    }
    app.clicked(that, 'signInFlag');

    app.sendRequest({
      url:'NsMemberSign.MemberSign.signIn',
      success:function(res){
        if (res.code == 0) {
          if(res.data > 0){
            app.showBox(that, '签到成功');
            that.setData({
              is_sign: 1
            })
          } else {
            app.showBox(that, res.message);
            app.restStatus(that, 'signInFlag');
          }
        } else {
          app.showBox(that, res.message);
          app.restStatus(that, 'signInFlag');
        }
      }
    })
  }
})